﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HROReporting.Infrastructure.Domain;
using HROReporting.Infrastructure.UnitOfWork;
using HROReporting.Model.Studenten;
using HROReporting.Services.Interfaces;
using HROReporting.Services.Mapping;
using HROReporting.Services.Messaging.StudentService;
using HROReporting.Services.ViewModels;
using HROReporting.Services.Messaging.SchoolLocatieService;
using HROReporting.Controllers.ViewModels.Vergader;

namespace HROReporting.Services.Implementations
{
    public class StudentService : IStudentService
    {
        private readonly IStudentRepository _studentRepository;

        private readonly IUnitOfWork _uow;

        public StudentService(IStudentRepository studentRepository, IUnitOfWork uow)
        {
            _studentRepository = studentRepository;
            _uow = uow;
        }
        /*
        public GetReserveringResponse GetReververingenByAuthenticationToken(GetStudentRequest request)
        {
            GetStudentResponse response = new GetStudentResponse();
            Student student = _studentRepository.FindBy(request.StudentIdentityToken);

            student.IdentityToken = request.StudentIdentityToken;

            GetReserveringResponse response2 = new GetReserveringResponse();

            Reservering allReserveringen = _studentRepository.GetReserveringenByAuthenticationToken(request.StudentIdentityToken);


            return response2;
        }
        */

        public GetAllStudentenResponse GetAllStudenten()
        {
            GetAllStudentenResponse response = new GetAllStudentenResponse();

            IEnumerable<StudentView> students = _studentRepository.FindAll().ConvertToStudentIndexView();

            response.Students = students;

            return response;
        }

        public CreateStudentResponse CreateStudent(CreateStudentRequest request)
        {
            CreateStudentResponse response = new CreateStudentResponse();
            Student student = new Student();

            student.IdentityToken = request.StudentIdentityToken;
            student.Email = request.Email;
            student.Studentnummer = student.Email.Substring(0, 7);

            ThrowExceptionIfStudentIsInvalid(student);

            _studentRepository.Add(student);
            _uow.Commit();

            response.Student = student.ConvertToStudentDetailView();

            return response;
        }

        private void ThrowExceptionIfStudentIsInvalid(Student student)
        {
            if (student.GetBrokenRules().Count() > 0)
            {
                StringBuilder brokenRules = new StringBuilder();
                brokenRules.AppendLine("Er zijn problemen ontstaan bij het opslaan van de Student: ");
                foreach (BusinessRule businessRule in student.GetBrokenRules())
                {
                    brokenRules.AppendLine(businessRule.Rule);
                }

                throw new StudentInvalidException(brokenRules.ToString());
            }
        }

        public GetStudentResponse GetStudent(GetStudentRequest request)
        {
            GetStudentResponse response = new GetStudentResponse();
            Student student = _studentRepository.FindBy(request.StudentIdentityToken);

            if (student != null)
            {
                response.StudentFound = true;
                response.Student = student.ConvertToStudentDetailView();
            }
            else
            {
                response.StudentFound = false;
            }

            return response;
        }

        public ModifyStudentResponse ModifyStudent(ModifyStudentRequest request)
        {
            ModifyStudentResponse response = new ModifyStudentResponse();
            Student student = _studentRepository.FindBy(request.StudentIdentityToken);

            student.Email = request.Email;
            student.Studentnummer = request.Studentnummer;

            ThrowExceptionIfStudentIsInvalid(student);

            _studentRepository.Save(student);
            _uow.Commit();

            response.Student = student.ConvertToStudentDetailView();

            return response;
        }
        /*
        public GetStudentResponse GetStudentByAuthenticationToken(GetStudentResponse request)
        {
            GetStudentResponse response = new GetStudentResponse();

            response.StudentFound = _studentRepository.GetStudentByAuthenticationToken(request.StudentFound);
            response.Student = _studentRepository.GetStudentByAuthenticationToken(request.AuthenticationToken).ConvertToStudentIndexView();

            return response;
        }
        
        public void CreateReservering(CreateReserveringRequest request)
        {
            IEnumerable<Reservering> allReserveringen = _.GetPresentielijstenBySerial(request.VergaderRuimte);

            int lesuur = Convert.ToInt32(request.LesuurEind.StartTijd.Replace(":", ""));
            foreach (Reservering lijst in allReserveringen)
            {
                if (lijst.Datum.Date == DateTime.Now.Date)
                {
                    if (lesuur <= Convert.ToInt32(lijst.LesuurEind.StartTijd.Replace(":", "")))
                    {
                        throw new InvalidOperationException("Er is al een reservering voor dit tijdstip.");
                    }
                }
            }

            Student student = request.User;

            Reservering reservering = new Reservering();
            reservering.User = request.User;
            reservering.VergaderRuimte = request.VergaderRuimte;
            reservering.LesuurBegin = request.LesuurStart;
            reservering.LesuurEind = request.LesuurEind;
            reservering.Datum = request.Datum;


            student.Reserveringen.Add(reservering);

            _studentRepository.Add(student);
            _uow.Commit();
        }

        public GetUserResponse GetUserResponseById(GetUserResponse request)
        {
            GetUserResponse response = new GetUserResponse();

            IEnumerable<UserView> users = _userRepository

            GetAllStudentenResponse response = new GetAllStudentenResponse();

            IEnumerable<StudentView> students = _studentRepository.FindAll().ConvertToStudentIndexView();

            response.Students = students;

            return response;
        }
        */


    }
}
